﻿using Pay.DTO;
using Pay.IService;
using Pay.TO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pay.Core;

namespace Pay.Web.Areas.Admin.Controllers {
    [RouteArea("admin")]
    [RoutePrefix("tokenShopCategory")]
    public class TokenShopCategoryController : AdminController {
        private ICategoryService _iCategoryService;
        private IMerchantService _iMerchantService;

        public TokenShopCategoryController(ICategoryService iCategoryService, IMerchantService iMerchantService) {
            _iCategoryService = iCategoryService;
            _iMerchantService = iMerchantService;
        }

        [HttpGet]
        public JsonResult Get(long id) {
            var result = new token_category_dto_ex();
            if (id > 0) {
                var dto = _iCategoryService.GetTokenCategory(id);
                dto.ToCopyProperty(result);
                result.image_urls = dto.image_urls;
            }
            result.parent_id_value = result.parent_id == 0 ? "" : result.parent_id.ToString();

            var firstCategoryList = _iCategoryService.GetTokenFirstDepthCatagory();
            if (firstCategoryList != null && firstCategoryList.Count() > 0) {
                foreach (var c in firstCategoryList)
                    result.first_category_list.Add(new DropdownItem() { Value = c.id.ToString(), Text = c.name });
                if (result.parent_id_value == "")
                    result.parent_id_value = result.first_category_list.FirstOrDefault().Value;
            }
            return Json(ResultToJson.Json(result), JsonRequestBehavior.AllowGet);
        }

        [HttpGet]
        public JsonResult Gets(CategoryQuery query) {
            var tokenMerchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            var model = _iCategoryService.GetCategoryPageList(query, tokenMerchant.id);
            return Json(ResultToJson.Json(model), JsonRequestBehavior.AllowGet);
        }

        [AcceptVerbs(HttpVerbs.Post | HttpVerbs.Put)]
        [ValidateInput(false)]
        public JsonResult Save(token_category_dto_ex model) {
            if (!string.IsNullOrEmpty(model.parent_id_value))
                model.parent_id = long.Parse(model.parent_id_value);
            var file_url = Upload("goods");
            if (!string.IsNullOrEmpty(file_url))
                model.image_urls = new List<string>() { file_url };
            var tokenMerchant = _iMerchantService.Get(t_RequestMethod.merchant_no);
            var result = _iCategoryService.AddOrUpdateTokenCategory(model, tokenMerchant.id);
            return Json(ResultToJson.Json(result ? "保存成功" : "保存失败", result), JsonRequestBehavior.AllowGet);
        }

        [HttpDelete]
        public JsonResult Delete(long id) {
            var result = _iCategoryService.RemoveTokenCategory(id);
            return Json(ResultToJson.Json(result ? " 删除成功" : "删除失败", result), JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult UpdateStatus(long id, int status) {
            var result = _iCategoryService.UpdateTokenCategoryStatus(id, status);
            return Json(ResultToJson.Json(result ? " 修改成功" : "修改失败", result));
        }
    }
}